package com.example.servicewallet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.servicewallet.entity.TUser;
import com.example.servicewallet.exception.MyException;
import com.example.servicewallet.mapper.TUserMapper;
import com.example.servicewallet.service.TUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.servicewallet.utils.JwtUtils;
import com.example.servicewallet.utils.MD5;
import com.example.servicewallet.vo.LoginVo;
import com.example.servicewallet.vo.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author admin
 * @since 2023-02-08
 */
@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements TUserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void register(RegisterVo registerVo) {
        //获取注册的数据
        String code = registerVo.getCode(); //验证码
        String mobile = registerVo.getMobile(); //手机号
        String nickname = registerVo.getNickname(); //昵称
        String password = registerVo.getPassword(); //密码

        //非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname)) {
            throw new MyException(20001, "注册失败");
        }
        //判断验证码
        //获取redis验证码
        //String redisCode = redisTemplate.opsForValue().get(mobile);
        //if (!code.equals(redisCode)) {
        //    throw new MyException(20001, "注册失败");
        //}

        //判断手机号是否重复，表里面存在相同手机号不进行添加
        QueryWrapper<TUser> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            throw new MyException(20001, "注册失败");
        }

        //数据添加数据库中
        TUser user = new TUser();
        user.setMobile(mobile);
        user.setNickname(nickname);
        user.setPassword(MD5.encrypt(password));//密码需要加密的
        user.setStatus(false);
        user.setUsername(nickname);
        baseMapper.insert(user);
    }

    @Override
    public String login(LoginVo user) {
        //获取登录手机号和密码
        String mobile = user.getMobile();
        String password = user.getPassword();

        //手机号和密码非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new MyException(20001, "登录失败");
        }

        //判断手机号是否正确
        QueryWrapper<TUser> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        TUser tUser = baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if (tUser == null) {//没有这个手机号
            throw new MyException(20001, "登录失败");
        }

        //判断密码
        //因为存储到数据库密码肯定加密的
        //把输入的密码进行加密，再和数据库密码进行比较
        //加密方式 MD5
        if (!MD5.encrypt(password).equals(tUser.getPassword())) {
            throw new MyException(20001, "登录失败");
        }

        //判断用户是否禁用
        if (tUser.getStatus()) {
            throw new MyException(20001, "登录失败");
        }

        //登录成功
        //生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(tUser.getId(), tUser.getNickname());
        redisTemplate.opsForValue().set(tUser.getUsername(),jwtToken,1, TimeUnit.DAYS);
        return jwtToken;
    }

    @Override
    public TUser selectUserByMobile(String mobile) {
        QueryWrapper<TUser> user = new QueryWrapper<>();
        user.eq("mobile",mobile);
        TUser tUser = baseMapper.selectOne(user);
        return tUser;
    }
}
